#!/sbin/openrc-run
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-emulation/xen-tools/files/xendomains.initd-r2,v 1.4 2012/01/12 13:40:01 alexxy Exp $

extra_commands="status"

depend() {
	need xenstored
	after dhcp xend xenconsoled
}

get_domname() {
	local name_from_file=$(sed -rn 's/^name\W*=\W*\"?([[:alnum:]_\.-]+)\"?\W*;?/\1/p' "${1}" | tail -n 1)

	if [ -z ${name_from_file} ]; then
		basename "${1}"
	else
		echo ${name_from_file}
	fi
}

is_running() {
	/usr/sbin/xl list "${1}" >/dev/null 2>&1
}

using_screen() {
	yesno "${SCREEN}"
}

set_screen_cmd() {
	export SCREENDIR=${SCREENDIR:-/tmp/screens/S-root}
	# create the parent dirs
	mkdir -p "$SCREENDIR"
	# set permissions
	checkpath -d -m 700 "$SCREENDIR"
	screen_cmd="screen -c ${SCREENRC:-/dev/null} -q -r ${SCREEN_NAME:=xen} -X"
}

wait_screen() {
	while ! ${screen_cmd} detach; do
		sleep 0.1
	done
}

init_screen() {
	set_screen_cmd
	ebegin "Creating screen session to hold domain consoles"
	(  screen -c ${SCREENRC:-/dev/null} -d -m -S ${SCREEN_NAME} -t dom0 \
		&& wait_screen \
		&& ${screen_cmd} zombie dr \
		&& logrotate -f /etc/xen/xen-consoles.logrotate \
		&& ${screen_cmd} logfile  /var/log/xen-consoles/%t.log \
		&& ${screen_cmd} logfile flush ${SCREEN_LOG_INTERVAL:-1} \
		&& ${screen_cmd} log on \
		&& ${screen_cmd} deflog on ) >/dev/null
	eend $?
}

startdom_screen() {
	${screen_cmd} screen -t ${1} xl create ${2} -c
}

close_screen() {
	set_screen_cmd
	if ${screen_cmd} sleep 0 >/dev/null 2>&1 ; then
		ebegin "Closing screen session ${SCREEN_NAME}"
		${screen_cmd} quit
		eend $?
	else
		eend 0
	fi
}

set_tmux_cmd() {
	: ${TMUX_NAME:=xen}
	tmux_cmd="tmux -f ${TMUX_CONF:-/dev/null}"
}

init_tmux() {
	set_tmux_cmd
	$tmux_cmd new-session -d  -s xen -n dom0 \; \
		set-option -g remain-on-exit on
}

startdom_tmux() {
	$tmux_cmd new-window -t ${TMUX_NAME:-xen} -n ${1} "xl create ${2} -c"
}

close_tmux() {
	set_tmux_cmd
	if ${tmux_cmd} has-session -t ${TMUX_NAME}; then
		ebegin "Closing tmux session ${TMUX_NAME}"
		$tmux_cmd kill-session -t ${TMUX_NAME}
		eend $?
	else
		eend 0
	fi
}
	
startdom_noconsole() {
	xl create --quiet ${2}
}


set_dom_cmd() {
	if using_screen; then
		: ${XENDOMAINS_CONSOLE:=screen}
	fi
	case "$XENDOMAINS_CONSOLE" in
	screen|tmux)
		initconsole=init_$XENDOMAINS_CONSOLE
		startdom=startdom_$XENDOMAINS_CONSOLE
		closeconsole=close_$XENDOMAINS_CONSOLE
		;;
	*)
		initconsole=
		startdom=startdom_noconsole
		closeconsole=
	esac
}

start() {
	set_dom_cmd
	checkpath --directory --mode 755 /run/xen

	einfo "Starting Xen domains from ${AUTODIR:=/etc/xen/auto}"

	$initconsole

	# Create all domains with config files in AUTODIR.
	want_usleep=
	for dom in $(ls "${AUTODIR:=/etc/xen/auto}/"* 2>/dev/null | sort); do
		name=$(get_domname ${dom})
		if ! is_running ${name} ; then
			if [ -n "$want_usleep" ]; then
				usleep ${XENDOMAINS_CREATE_USLEEP:=5000000}
			else
				want_usleep=1
			fi
			ebegin "  Starting domain ${name}"
			$startdom "${name}" "${dom}"
			eend $?
		else
			einfo "  Not starting domain ${name} - already running"
		fi
	done
}

stop() {
	set_dom_cmd

	einfo "Shutting down Xen domains from ${AUTODIR:=/etc/xen/auto}"
	# Stop all domains with config files in AUTODIR.
	DOMAINS="$(ls "${AUTODIR:=/etc/xen/auto}/"* 2>/dev/null | sort -r)"

	if yesno "$PARALLEL_SHUTDOWN"; then
		for dom in $DOMAINS ; do
			name=$(get_domname ${dom})
			if is_running ${name} ; then
				ebegin "  Asking domain ${name} to shutdown in the background"
				xl shutdown -w ${name} >/dev/null &
			else
				einfo "  Not stopping domain ${name} - not running"
			fi
		done
		einfo "  Waiting for shutdown of domains that are still running"
		wait
		eend $?
	else
		for dom in $DOMAINS ; do
			name=$(get_domname ${dom})
			if is_running ${name} ; then
				ebegin "  Waiting for domain ${name} to shutdown"
				xl shutdown -w ${name} >/dev/null
				eend $?
			else
				einfo "  Not stopping domain ${name} - not running"
			fi
		done
	fi

	$closeconsole
}

status() {
	/usr/sbin/xl list
}
